package com.example.backend.controller;

import com.example.backend.annotation.RateLimit;
import com.example.backend.entity.Point;
import com.example.backend.mapper.PointMapper;
import com.example.backend.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/poi")
public class PointController {

    private final PointMapper pointMapper;
    public PointController(PointMapper pointMapper) {this.pointMapper = pointMapper;}

    // 查询窗口内的武汉街景点
    @RateLimit
    @GetMapping("/window/wuhan")
    public Result<List<Point>> select_Wuhan_Window(
            @RequestParam("minX") double minX,
            @RequestParam("minY") double minY,
            @RequestParam("maxX") double maxX,
            @RequestParam("maxY") double maxY)
    {
        try {
            List<Point> pointList = pointMapper.select_Wuhan_Window(minX, minY, maxX, maxY);
            if (pointList != null)
                return Result.success(pointList);
            else
                return Result.error("404", "empty result: poi");
        }
        catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    // 查询窗口内的武汉街景点
    @RateLimit
    @GetMapping("/window/community")
    public Result<List<Point>> select_user_window(
            @RequestParam("minX") double minX,
            @RequestParam("minY") double minY,
            @RequestParam("maxX") double maxX,
            @RequestParam("maxY") double maxY)
    {
        try {
            List<Point> pointList = pointMapper.select_user_window(minX, minY, maxX, maxY);
            if (pointList != null)
                return Result.success(pointList);
            else
                return Result.error("404", "empty result: poi");
        }
        catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

    // 查询待审核的街景点
    @PreAuthorize("hasAuthority('admin')")
    @GetMapping("/examine")
    public Result<List<Point>> select_user_examine(){
        try {
            List<Point> pointList = pointMapper.select_user_examine();
            if (pointList != null)
                return Result.success(pointList);
            else
                return Result.error("404", "empty result: poi");
        }
        catch (Exception e) {
            return Result.error("500", e.getMessage());
        }
    }

}


